package com.sangmo.fts;


import com.sangmo.boot.framework.common.dict.DictionaryManager;
import com.sangmo.boot.framework.common.i18n.MessageMaker;
import com.sangmo.boot.framework.data.constraint.ConstraintChecker;
import com.sangmo.boot.framework.data.entity.audit.UserSessionAuditor;
import com.sangmo.boot.framework.data.filter.SimpleQuery;
import com.sangmo.boot.framework.data.json.load.impl.JPALoader;
import com.sangmo.boot.framework.data.repo.impl.EntityRepositoryFactoryBean;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import java.io.Serializable;

//@Configuration
//@EnableJpaRepositories(basePackages="com.sangmo",
//        repositoryFactoryBeanClass = EntityRepositoryFactoryBean.class
//)
//@EntityScan(basePackages="com.sangmo")
//@EnableJpaAuditing(auditorAwareRef = "auditorAware")
public class JpaConfig {

    @Bean
    public AuditorAware<Serializable> auditorAware() {
        return new UserSessionAuditor();
    }

    @Bean
    public JPALoader jpaLoader() {
        return new JPALoader();
    }

    @Bean
    public ConstraintChecker constraintChecker() {
        return new ConstraintChecker();
    }

    @Bean
    public SimpleQuery simpleQuery() {
        return new SimpleQuery();
    }

    @Bean
    public DictionaryManager dictionaryManager(MessageMaker messageMaker) throws Exception {
        DictionaryManager dictionaryManager = new DictionaryManager();
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources("dictionary/*.json");
        dictionaryManager.setMessageMaker(messageMaker);
        dictionaryManager.init(resources);
        return dictionaryManager;
    }
}
